package com.visionet.waveletData.web.web.group;

import com.visionet.waveletData.web.service.account.ShiroDbRealm;
import com.visionet.waveletData.web.service.group.GroupService;
import com.visionet.waveletData.web.vo.GroupVo;
import com.visionet.waveletData.web.vo.RobotGroupVo;
import com.visionet.waveletData.web.vo.RobotVo;
import com.visionet.waveletData.web.web.BaseController;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hxpeng on 2017/1/10.
 */

@Controller
@RequestMapping(value = "/group")
public class GroupController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(GroupController.class);

    @Autowired
    private GroupService groupService;

    @RequestMapping(value = "/groupList", method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> groupList(@RequestBody GroupVo groupVo){
        Map<String,Object> result = new HashMap<>();

        ShiroDbRealm.ShiroUser suser = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        if(suser == null){
            throw new RuntimeException("未登录");
        }
        groupVo.setCompanyId(suser.getCompanyId());
        try {
            Page<GroupVo> page  = groupService.findAllGroupVo(groupVo);
            if(page != null && page.getContent() != null){
                result.put("page",page);
            }
        }catch (Exception e){
            logger.error("查询所有分组异常");
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "/createNewGroup", method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> createNewGroup(@RequestBody GroupVo groupVo){
        if(groupVo == null || StringUtils.isBlank(groupVo.getGroupName())){
            throw new NullPointerException("新建分组，参数为空！");
        }
        ShiroDbRealm.ShiroUser suser = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        if(suser == null){
            throw new RuntimeException("未登录");
        }
        groupVo.setCompanyId(suser.getCompanyId());
        Map<String,Object> map = new HashMap<>();
        try{
            Long groupId = groupService.createNewGroup(groupVo);
            map.put("result","1");
            map.put("groupId",groupId);
        }catch (RuntimeException e){
            e.printStackTrace();
            map.put("msg",e.getMessage());
            map.put("result","err");
        }catch (Exception e){
            e.printStackTrace();
            map.put("result","err");
            map.put("msg",e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/groupDetail/{groupId}", method = RequestMethod.GET)
    public @ResponseBody Map<String,Object> groupDetail(@PathVariable String groupId){
        if(StringUtils.isBlank(groupId)){
            throw new NullPointerException("查询分组详情，参数为空！！");
        }
        Map<String,Object> map = new HashMap<>();
        try {
            GroupVo result = groupService.findOneByGroupId(Long.parseLong(groupId));
            map.put("stat","success");
            map.put("result",result);
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return map;
    }


    @RequestMapping(value = "/groupDelete/{groupId}", method = RequestMethod.GET)
    public @ResponseBody Map<String,Object> groupDelete(@PathVariable String groupId) {
        if(StringUtils.isBlank(groupId)){
            throw new NullPointerException("删除分组，参数为空！！");
        }
        Map<String,Object> map = new HashMap<>();
        try {
            groupService.robotGroupDelete(groupId);
            map.put("result","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("result","err");
            map.put("msg",e.getMessage());
        }
        return map;
    }



    @RequestMapping(value = "/groupDetailRobotPage", method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> groupDetailRobotPage(@RequestBody GroupVo groupVo){
        if(groupVo == null || groupVo.getGroupId().isEmpty()){
            throw new NullPointerException("查询分组详情机器热列表，参数为空！！");
        }
        Map<String,Object> map = new HashMap<>();
        try {
            PageImpl<RobotVo> result = groupService.findRobotPageByGroupId(groupVo.getGroupId(),groupVo.getPageInfo());
            map.put("stat","success");
            map.put("result",result);
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return map;
    }

    @RequestMapping(value = "/groupDeleteRobot", method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> delete(@RequestBody RobotGroupVo robotGroupVo){
        if(StringUtils.isBlank(robotGroupVo.getGroupId()) || StringUtils.isBlank(robotGroupVo.getRobotId())){
            throw new NullPointerException("删除分组内，需要请求参数为空！！！");
        }
        Map<String,Object> map = new HashMap<>();
        try{
            map.put("result",groupService.groupDeleteRobot(robotGroupVo.getGroupId(),robotGroupVo.getRobotId()));
        }catch (Exception e){
            logger.error("删除机器人异常：" + e.getMessage());
            map.put("result","err");
        }
        return map;
    }


    @RequestMapping(value = "/saveRobotGroupInfo", method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> saveRobotGroupInfo(@RequestBody GroupVo groupVo){
        if(StringUtils.isBlank(groupVo.getGroupId()) || StringUtils.isBlank(groupVo.getGroupName())){
            throw new NullPointerException("保存分组信息，需要请求参数为空！！！");
        }
        ShiroDbRealm.ShiroUser suser = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        if(suser == null){
            throw new RuntimeException("未登录");
        }
        groupVo.setCompanyId(suser.getCompanyId());
        Map<String,Object> map = new HashMap<>();
        try{
            groupService.saveGroupInfo(groupVo);
            map.put("result","1");
        }catch (Exception e){
            map.put("result","err");
            map.put("msg",e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/addRobotInGroup", method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> addRobotInGroup(@RequestBody RobotGroupVo robotGroupVo){
        if(robotGroupVo == null || StringUtils.isBlank(robotGroupVo.getGroupId()) || StringUtils.isBlank(robotGroupVo.getRobotId())){
            throw new NullPointerException("向分组添加机器人参数为空！！");
        }
        Map<String,Object> map = new HashMap<>();
        try {
            groupService.addRobotToGroup(robotGroupVo);
            map.put("result","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("result","err");
            map.put("msg",e.getMessage());
        }
        return map;
    }
}
